package com.hundsun.s3.platform.backstage.service.Impl;

import com.hundsun.jresplus.common.util.StringUtil;
import com.hundsun.s3.common.dto.DepartmentDTO;
import com.hundsun.s3.common.dto.DeptDTO;
import com.hundsun.s3.common.dto.Message;
import com.hundsun.s3.common.dto.SimpleMsg;
import com.hundsun.s3.common.util.BaseDao;
import com.hundsun.s3.platform.backstage.common.util.TransformatUtil;
import com.hundsun.s3.platform.service.CommonService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * com.hundsun.s3.platform.backstage.service.Impl
 *
 * @Description:
 * @Author: yangjun23614
 * @Date: Created in 14:03 2018/3/8
 * V1.0 14:03 2018/3/8
 */
@Service
public class CommonServiceImpl extends BaseDao implements CommonService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private ChatService chatClientService;

    /**
     * @Description: 获取部门所有
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 9:13 2018/3/12
     * V1.0
     */
    @Override
    public List<DepartmentDTO> getAllDeptList() {
        List<DeptDTO> listDeptDto= this.qry("800017",new HashMap<String, Object>(),DeptDTO.class);
        List<DepartmentDTO> list = new ArrayList<>();
        for (DeptDTO d : listDeptDto) {
            DepartmentDTO departmentDTO = new DepartmentDTO();
            departmentDTO.setId(d.getId());
            departmentDTO.setName(d.getDept_name());
            departmentDTO.setpId(d.getParent_id());
            departmentDTO.setDept_order(d.getDept_order());
            list.add(departmentDTO);
        }
        return list;
    }

    /**
     * @Description:  根据id获取第几级别的部门
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 9:13 2018/3/12
     * V1.0
     */
    @Override
    public int getDeptGradeByDeptNo(int deptNo) {
        int deptGrade = 1;
        List<DeptDTO> listDeptDto= this.qry("800017",new HashMap<String, Object>(),DeptDTO.class);
        int deptPId = 0;
        for (DeptDTO d:listDeptDto) {
            if(d.getId() == deptNo){
                deptPId = d.getParent_id();
                break;
            }
        }
        while (deptPId != 0) {
            for (DeptDTO d:listDeptDto) {
                if(d.getId() == deptPId){
                    deptGrade++;
                    deptPId = d.getParent_id();
                    break;
                }
            }
        }
        return deptGrade;
    }

    /**
     * @Description: 拼接部门为四位字符串
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 9:18 2018/3/12
     * V1.0
     */
    @Override
    public String mergeDept(int deptNo) {
        List<DepartmentDTO> departmentDTOs = this.getAllDeptList();
        String result = TransformatUtil.integerToString(deptNo);
        int deptPId = 0;
        for (DepartmentDTO dept:departmentDTOs) {
            if(dept.getId() == deptNo) {
                deptPId = dept.getpId();
            }
        }
        while (deptPId != 0) {
            for (DepartmentDTO dept:departmentDTOs) {
                if(dept.getId() == deptPId) {
                    result = TransformatUtil.integerToString(deptPId) + result;
                    deptPId = dept.getpId();
//                    if(deptPId == 0) {break;}
                }
            }
        }
        return result;
    }

    /**
     * @Description: 聊天记录
     * @param:
     * @return:
     * @Author: yangjun23614
     * @Date: 11:06 2018/3/14
     * V1.0
     */
    @Override
    public List<SimpleMsg> getChatRecordByIdTime(String fromId, String toId, Long serverStartTime, Long serverEndTime) {
        /*Query query =Query.query(new Criteria().orOperator(Criteria.where("fromId").is(fromId).and("toId").is(toId),
                Criteria.where("toId").is(fromId).and("fromId").is(toId)));
        query.with(new Sort(Sort.Direction.DESC,"timestamp"));
        List<Message> messages = mongoTemplate.find(query,Message.class,fromId);*/
        List<Message> messages = chatClientService.readAllRecord(fromId,toId);
        List<SimpleMsg> simpleMsgs = new ArrayList<>();
        for (Message message:messages) {
            if(StringUtil.isNotEmpty(message.getTimestamp())) {
                SimpleMsg simpleMsg = new SimpleMsg();
                simpleMsg.setFromName(message.getFromName());
                simpleMsg.setMsg(message.getMsg());
                simpleMsg.setMime(message.getMime());
                simpleMsg.setTime(TransformatUtil.timestampToLong(message.getTimestamp()));
                simpleMsgs.add(simpleMsg);
            }

        }
        List<SimpleMsg> simpleMsgInTime = new ArrayList<>();
        for (SimpleMsg simpleMsg:simpleMsgs) {
            if(simpleMsg.getTime() >= serverStartTime && simpleMsg.getTime() <= serverEndTime) {
                simpleMsgInTime.add(simpleMsg);
            }
        }
        return simpleMsgInTime;
    }
}
